<?php
/**
 * Created by PhpStorm.
 * User: *!N.J
 * Date: 2021/8/22
 * Time: 0:19
 * QQ:1467572213
 */

namespace app\sr\controller;

use think\Controller;
use think\facade\Request;
use think\facade\Session;
use \app\index\model\Login as Login_m;
use think\Db;

class Login extends Controller
{
    public function index()
    {
        if (Request::isPost()){
            $info['account'] = Request::post('account');
            $info['password'] = Request::post('password');
            $login = Login_m::login($info);
            if ($login == 'error_status') return ['code'=>0,'msg'=>'This account has been blocked'];
            if ($login != 'error'){
                Session::set('user_id',$login['id']);
                return ['code'=>1,'msg'=>'Success'];
            }else{
                return ['code'=>0,'msg'=>'The account does not exist or the password is incorrect'];
            }
        }
        return $this->fetch();
    }

    public function reg()
    {
        $invite = Request::route(true);
        if ($invite){
            $this->assign('invite',$invite);
        }
        if (Request::isPost()){
            $info['username'] = Request::post('username');
            if (empty($info['username'])){
                return ['code'=>0,'msg'=>'Please enter the account number'];
            }
            $check_username = Login_m::check_username($info['username']);
            if ($check_username == 'error') return ['code'=>0,'msg'=>'This account already exists'];

            $phone = Request::post('phone');
            if (empty($phone)){
                return ['code'=>0,'msg'=>'Please enter your mobile phone number'];
            }
            $check_phone = Login_m::check_phone($phone);
            if ($check_phone == 'error') return ['code'=>0,'msg'=>'The phone number already exists'];

            $code = Request::post('sms_code');
            // print_r($code);die;
            if(session('reg_sms_code_'.$phone)!=$code){
                return ['code'=>0,'msg'=>'Verification code error'];
            }
            // $check_code = Login_m::check_code($code);
            //  print_r($check_code);die;
            // if ($check_code == 'error'){
            //     return ['code'=>0,'msg'=>'Verification code error'];
            // } else{
            //     $info['phone'] = $check_code;
            // }
            $password = Request::post('password');
            if (!empty($password)){
                $info['password'] = md5($password);
            }else{
                return ['code'=>0,'msg'=>'Login password cannot be empty'];
            }
            $payment = Request::post('payment');
            if (!empty($payment)){
                $info['payment'] = md5($payment);
            }else{
                return ['code'=>0,'msg'=>'Payment password cannot be blank'];
            }
            $info['phone'] = $phone;
            $invite_code = Request::post('invite_code');
            if(empty($invite_code)){
                return ['code'=>0,'msg'=>'invite_code  cannot be empty'];
            }
            $pid=Login_m::get_pid_info($invite_code);
            if($pid==0){
                return ['code'=>0,'msg'=>'invite_code error'];
            }
            $info['pid'] = $pid;

            $info['invite_code'] = Login_m::get_invite_code();
            $info['group'] = 51;
            $info['balance'] = Db::name('system_config')->where('name','cjrg')->value('value');
            $info['total'] = 0;
            $info['financial'] = 0;
            $info['freeze'] = 0;
            $info['level'] = 7;
            $info['auto_grab'] = 0;
            $info['reg_ip'] = get_ip();
            $info['last_time'] = time();
            $info['create_time'] = time();
            $reg = Login_m::reg($info,$code);
            switch ($reg)
            {
                case 'success':
                    return ['code'=>1,'msg'=>'Success'];
                    break;
                case 'error':
                    return ['code'=>0,'msg'=>'Error'];
                    break;
            }
        }
        return $this->fetch();
    }

    public function sms_code()
    {
        if (Request::isPost()){
            $phone = Request::post('phone');
            $type = Request::post('type');
            $sms_code = Login_m::sms_code($phone,$type);
            if ($sms_code['code'] == 'success'){
                return ['code'=>1,'msg'=>'Sent successfully'];
            }else{
                return ['code'=>0,'msg'=>'Error'];
            }
        }
    }

    public function check_username()
    {
        if (Request::isPost()){
            $username = Request::post('username');
            $check = Login_m::check_username($username);
            switch ($check)
            {
                case 'success':
                    return ['code'=>1,'msg'=>'Success'];
                    break;
                case 'error':
                    return ['code'=>0,'msg'=>'This account already exists'];
                    break;
            }
        }
    }

    //发送短信
    public function check_phone()
    {
        if (Request::isPost()){
            $phone = Request::post('phone');
            $check = Login_m::check_phone($phone);
            switch ($check)
            {
                case 'success':
                    $time = date('YmdHis');
                    $reqUrl = "http://8.219.176.7:8088/abroad/my/v2/send";
                    $sign = md5('456674'.'123456'.$time);
                    $code = rand(1000,9999);
                    session('reg_sms_code_'.$phone, $code);
                    $postdata=[
                        "userid"    =>'456674',
                        "timestamp"    =>$time,
                        "sign"    => $sign,
                        'mobile'=>'52'.$phone,
                        "senderId"    =>'',
                        "content"    =>'your code is '.$code,
                        'sendTime'=>'',
                        'extno'=>'',
                    ];
                    // print_r($postdata);die;
                    $curl = curl_init();
                    curl_setopt($curl, CURLOPT_URL, $reqUrl);
                    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3);
                    curl_setopt($curl, CURLOPT_TIMEOUT, 3);
                    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
                    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
                    curl_setopt($curl, CURLOPT_POST, 1);
                    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postdata));
                    curl_setopt($curl, CURLOPT_HEADER, 0);
                    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                        'Content-Type: application/x-www-form-urlencoded;charset=utf-8',
                        'Cache-Control: no-cache',
                        'Pragma: no-cache'
                    ));
                    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
                    $response = curl_exec ($curl);
                    // print_r($response);die;
                    curl_close($curl);
                    
                    return ['code'=>1,'msg'=>'Success'];
                    break;
                case 'error':
                    return ['code'=>0,'msg'=>'The phone number already exists'];
                    break;
            }
        }
    }

    public function forget()
    {
        if (Request::isPost()){
            $info['phone'] = Request::post('phone');
            $info['code']= Request::post('code');
            $info['password']= Request::post('password');
            $forget = Login_m::forget($info);
            switch ($forget)
            {
                case 'success':
                    return ['code'=>1,'msg'=>'Success'];
                    break;
                case 'error':
                    return ['code'=>0,'msg'=>'Error'];
                    break;
                case 'error_code':
                    return ['code'=>0,'msg'=>'Verification code error'];
                    break;
            }
        }
        return $this->fetch();
    }

    public function base_to_img()
    {
        $base64 = "iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAABaklEQVQ4T62VwU7CQBCGZ7bU7aGkhDZNNBxMfAR9EA/qRR8BY+KDmBh5BE/iwQfRRzB4IBpKS2ng0AXaMUNaQhCQhJ1Lk2b2m/33n51FWBNEdBAEwSUiniPiKQAcFWnfRPRBRG++77cRcbK6HFd/FKAH0zQbUkowTROEEPO0PM9hOp2CUoq/XSK6Z/AyYwEkIhFF0aMQomnb9hy0LRg8Ho+5SMt13TtEzDl/AQzD8MkwjKbjOID4Z+Nr2UQESZJAlmUtz/NuF0CWWalUXmq12s6wsgJDh8MhzGazq/m5sgFhGH46jtP4T+amI2D5SZJ0Pc87wV6vdy2lfGap+wRLV0rdYBAE7Wq1emFZ1j48SNMURqPRKwM79Xr92DCMvYBZlsFgMPjCfr+fuq4rd3V2U1U2J4oipR+oU3Icxx39pmhvG+2Nza5pvXplG2gdDgzVPr7KnWobsMs3YPkJAIAzRDwsVPwAwPu2J+AXZ1wX1jVIDg0AAAAASUVORK5CYII=";
        $img = base64_decode($base64);
        file_put_contents('./iii/b7.png', $img);
    }
    
    public function huoqu()
    {
        $this->title = '生成';
        $keyword = input('get.keyword/s','');

        if(!empty($keyword)){
            $where['username'] = $keyword;
        }
        
        $userinfo = Db::name('fd_user')->field('id,bank_name,account_number')->where($where)->find();
        
        $arr['code'] = 0;
        $arr['msg'] = "成功";
        $arr['data'] = $userinfo;
        echo json_encode($arr);die;
        // echo ['code'=>0,'msg'=>'手动充值失败','data'=>$userinfo];
    }
    
    public function info(){
        $id = input('get.id/s','');
        
        if(!empty($id)){
            $where['id'] = $id;
        }
        
        $userinfo = Db::name('fd_user')->where($where)->find();
        
        $arr['code'] = 0;
        $arr['msg'] = "成功";
        $arr['data'] = $userinfo;
        echo json_encode($arr);die;
    }

}